package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.AddressBook;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.service.AddressBookService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    /**
     * 添加收货地址
     * @param addressBook
     * @return
     */
    @Override
    public JsonResult add(AddressBook addressBook) {
        // 添加
        save(addressBook);
        // 设置id条件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", addressBook.getId());
        // 根据id获取
        AddressBook one = getOne(wrapper);

        return new JsonResult(1, null, one,null);
    }

    /**
     * 修改收货地址
     * @param addressBook
     * @return
     */
    @Override
    public JsonResult put(AddressBook addressBook) {
        // 条件id
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", addressBook.getId());

        // 根据id修改
        update(addressBook, wrapper);

        return JsonResult.success("修改成功");
    }

    /**
     * 删除收货地址
     * @param ids
     * @return
     */
    @Override
    public JsonResult delete(String ids) {
        String[] split = ids.split(",");
        // 数组转集合
        List<String> id = Arrays.asList(split);

        removeByIds(id);
        return JsonResult.success("删除成功");
    }

    /**
     * 获取默认地址
     * @return
     */
    @Override
    public JsonResult def() {
        // 获取默认值条件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_default", 1);

        AddressBook one = getOne(wrapper);

        return JsonResult.success(one);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @Override
    public JsonResult setdef(AddressBook addressBook) {
        //其他的地址默认值要设置成0
        List<AddressBook> list = list();
        for (AddressBook book : list) {
            if (book.getId().equals(addressBook.getId())){
                book.setIsDefault(1);
                continue;
            }
            book.setIsDefault(0);
        }
        updateBatchById(list);

        AddressBook setreturn = getById(addressBook.getId());

        return JsonResult.success(setreturn);
    }

    /**
     * 获取收货地址列表
     * @return
     */
    @Override
    public JsonResult addressBookList() {
        List<AddressBook> list = list();

        return JsonResult.success(list);
    }

    /**
     * 根据id获取收货地址
     * @param id
     * @return
     */
    @Override
    public JsonResult getAddressBookById(String id) {
        AddressBook addressBook = getById(id);

        return JsonResult.success(addressBook);
    }
}
